project(syncup)
cmake_minimum_required(VERSION 2.8)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake_module)
set(CMAKE_C_FLAGS_DEBUG "-Wall -g -DDEBUGGING")

include(CheckIncludeFile)
include(CheckFunctionExists)

include_directories(include)

find_package(Threads REQUIRED)
if (NOT CMAKE_USE_PTHREADS_INIT)
	message(FATAL_ERROR "pthread is required")
endif()

find_package(PkgConfig)

if (PKG_CONFIG_FOUND)
	pkg_check_modules(GLIB2 REQUIRED glib-2.0 gobject-2.0 gthread-2.0)
else()
	find_package(BundleGlib2)
endif()
if (BUNDLE_GLIB2_FOUND)
	link_directories(${GLIB2_LIBRARY_DIRS})
endif()
include_directories(${GLIB2_INCLUDE_DIRS})

check_function_exists(clock_gettime HAVE_CLOCK_GETTIME)
check_include_file("sys/time.h" HAVE_SYS_TIME_H)
if (HAVE_SYS_TIME_H)
	check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
endif()
check_include_file("sys/select.h" HAVE_SYS_SELECT_H)

if (WIN32)
	set(OS_WIN TRUE)
endif()

configure_file(include/syncupconfig.h.in include/syncupconfig.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)

if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
	# make sure both compile and link with multithread support
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
	include_directories(include/os/linux)
	add_subdirectory(os/linux)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mms-bitfields")
	include_directories(include/os/windows)
	add_subdirectory(os/windows)
endif()

add_subdirectory(syncup)
add_subdirectory(test)
